/**
 * Copyright (C), 2002-2017, 上海巧房信息技术有限公司
 * FileName: CollectRuleFacadeImpl.java
 * @Author:   yi.li
 * Date:     2018年6月29日 下午2:43:07
 * Description:  采集规则
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.qiaofang.collectmanage.facade.impl;


import com.qiaofang.collectmanage.facade.utils.StringUtils;
import com.qiaofang.collectmanage.service.CollectRuleService;
import com.qiaofang.collectmanage.stub.bean.CollectRuleDTO;
import com.qiaofang.collectmanage.stub.bean.SearchCollectRulePageDTO;
import com.qiaofang.collectmanage.stub.constants.CollectRuleResponseCode;
import com.qiaofang.collectmanage.stub.facade.CollectRuleFacade;
import com.qiaofang.collectmanage.stub.request.*;
import com.qiaofang.collectmanage.stub.response.*;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.response.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
public class CollectRuleFacadeImpl implements CollectRuleFacade {

	@Autowired
	private CollectRuleService collectRuleService;

	@Override
	public GetCollectRuleByUuidResponse getCollectRuleById(@RequestBody @Valid GetCollectRuleByUuidRequest request) {
		GetCollectRuleByUuidResponse response = new GetCollectRuleByUuidResponse();
		CollectRuleDTO dto = collectRuleService.getCollectRuleById(request.getId());
		response.setData(dto);
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}

	@Override
	public CommonResponse saveCollectRule(@RequestBody @Valid SaveCollectRuleRequest request) {
		collectRuleService.saveCollectRule(request.getSaveCollectRuleDTO());
		return new CommonResponse();
	}

	@Override
	public CommonResponse updateCollectRule(@RequestBody @Valid UpdateCollectRuleRequest request) {
		collectRuleService.updateCollectRule(request.getUpdateCollectRuleDTO());
		return new CommonResponse();
	}

	@Override
	public CommonResponse removeCollectRule(@RequestBody  @Valid  RemoveCollectRuleRequest request) {
		collectRuleService.removeCollectRule(request.getRemoveCollectRuleDTO());
		return new CommonResponse();
	}

	@Override
	public ListCollectRuleResponse listCollectRule(@RequestBody @Valid ListCollectRuleRequest request) {
		ListCollectRuleResponse response = new ListCollectRuleResponse();
		List<CollectRuleDTO> data = collectRuleService.listCollectRule(request.getCollectRuleDTO());
		response.setData(data);
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}


	@Override
	public GetCollectRuleResponse getCollectRule(@RequestBody @Valid GetCollectRuleRequest request) {
		GetCollectRuleResponse response = new GetCollectRuleResponse();
		CollectRuleDTO collectRuleDTO  = collectRuleService.getCollectRule(request.getGetCollectRuleDTO());
		response.setData(collectRuleDTO);
		return response;
	}



	@Override
	public SearchCollectRulePageResponse searchCollectRulePage(@RequestBody @Valid SearchCollectRulePageRequest request) {
		SearchCollectRulePageResponse response = new SearchCollectRulePageResponse();
		List<CollectRuleDTO> list = collectRuleService.searchCollectRulePage(request.getSearchCollectRulePostDTO(),request.getPageDTO());
		SearchCollectRulePageDTO searchCollectRulePageDTO = new SearchCollectRulePageDTO();
		searchCollectRulePageDTO.setData(list);
		searchCollectRulePageDTO.setCount(collectRuleService.countSearchCollectRulePage(request.getSearchCollectRulePostDTO()));
		response.setData(searchCollectRulePageDTO);
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}


	@Override
	public CommonResponse commonCityExtend(@RequestBody @Valid  CommonCityExtendRequest request) {
		CommonResponse response = new CommonResponse();
		collectRuleService.commonCityExtend(request.getCity(),request.getSource(),request.getBelonger(),request.getCollectType(),request.getCollectHouseType(),request.getData());
		return response;
	}


	@Override
	public ResolveRuleResponse resolveRule(@RequestBody @Valid  ResolveRuleRequest request){
        ResolveRuleResponse response = new ResolveRuleResponse();
        try {
			response.setData(collectRuleService.startData(request.getCity(), request.getSource(), request.getBelonger(), request.getCollectType(), request.getCollectHouseType(), request.getTitleLink()));
		}catch(Exception e){
			throw new BusinessException(CollectRuleResponseCode.COLLECT_FAIL.getResponseCode(), StringUtils.getStackTrace(e));
        }
		response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
		response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
		return response;
	}


	@Override
	public GetCitysResponse getCitys() {
		GetCitysResponse getCitysResponse = new GetCitysResponse();
		getCitysResponse.setData(collectRuleService.getCitys());
		return getCitysResponse;
	}

	@Override
	public GetSourcesResponse getSources() {
		GetSourcesResponse getSourcesResponse = new GetSourcesResponse();
		getSourcesResponse.setData(collectRuleService.getSources());
		return getSourcesResponse;
	}



}
